﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class QuyenProvider
    {
        public static List<Quyen> LayDanhSachQuyen()
        {
            var lst = new List<Quyen>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.Quyens select u);

                foreach (var row in query)
                {
                    var quyen = new Quyen();
                    quyen.MaQuyen = row.MaQuyen;
                    quyen.MaNhomQuyen = row.MaNhomQuyen;
                    quyen.TenQuyen = row.TenQuyen;

                    lst.Add(quyen);
                }
            }

            return lst;
        }

        public static List<Quyen> LayDanhSachQuyen(string username)
        {
            var lst = new List<Quyen>();

            var maNhomND = NhanVienProvider.LayNguoiDungTheoUsername(username).MaNhomND;
            var phanQuyen = PhanQuyenProvider.LayPhanQuyenTheoMaNhomNguoiDung(maNhomND);

            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.Quyens select u)
                    .Where(t => t.MaNhomQuyen.Equals(phanQuyen.MaNhomQuyen) && t.Active);

                foreach (var row in query)
                {
                    var quyen = new Quyen();
                    quyen.MaQuyen = row.MaQuyen;
                    quyen.MaNhomQuyen = row.MaNhomQuyen;
                    quyen.TenQuyen = row.TenQuyen;

                    lst.Add(quyen);
                }
            }

            return lst;
        }

        public static List<Quyen> LayDanhSachQuyen(int maNhomQuyen)
        {
            var lst = new List<Quyen>();

            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.Quyens select u)
                    .Where(t => t.MaNhomQuyen.Equals(maNhomQuyen) && t.Active);

                foreach (var row in query)
                {
                    var quyen = new Quyen();
                    quyen.MaQuyen = row.MaQuyen;
                    quyen.MaNhomQuyen = row.MaNhomQuyen;
                    quyen.TenQuyen = row.TenQuyen;

                    lst.Add(quyen);
                }
            }

            return lst;
        }

        public static bool XoaQuyen(int maQuyen)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {

                    var query = (from u in context.Quyens
                                 where u.MaQuyen.Equals(maQuyen)
                                 select u).First();

                    context.Quyens.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool XoaQuyen(int maNhomQuyen, string tenQuyen)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {

                    var query = (from u in context.Quyens
                                 where u.MaNhomQuyen.Equals(maNhomQuyen)
                                 select u)
                                 .Where(t => t.TenQuyen.Equals(tenQuyen))
                                 .First();

                    context.Quyens.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CapNhatQuyen(Quyen quyen)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.Quyens
                                 where u.MaNhomQuyen.Equals(quyen.MaQuyen)
                                 select u).First();
                    query.MaNhomQuyen = quyen.MaNhomQuyen;
                    query.TenQuyen = quyen.TenQuyen;

                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CapNhatTrangThaiQuyen(int manhom, string quyen, bool status)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.Quyens select u)
                                .Where(t => t.MaNhomQuyen.Equals(manhom) &&
                                t.TenQuyen.Equals(quyen))
                                .First();
                    query.Active = status;
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static int ThemQuyen(Quyen quyen)
        {
            int maQuyen = -1;
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    context.Quyens.Add(quyen);
                    context.SaveChanges();
                    maQuyen = quyen.MaQuyen;
                }
            }
            catch
            {

            }
            return maQuyen;
        }
    }
}
